<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Game extends Model
{
    //
    protected $fillable = ['name', 'code', 'category_id', 'grade_id'];

    public function category(){
        return $this->belongsTo(Category::class);
    }

    public function grade(){
        return $this->belongsTo(Grade::class);
    }
    public function ChildStars(){
        return $this->hasMany(ChilChildStar::class, 'code', 'code');
    }
    /*

* 拼接查询条件
*      */
    public static function selectQuery($data){
        return function ($query) use ($data) {
            if(isset($data['category_id']) && $data['category_id'] > 0){
                $query->where('category_id', $data['category_id']);
            }
            if(isset($data['grade_id']) && $data['grade_id'] > 0){
                $query->where('grade_id', $data['grade_id']);
            }
            if (isset($data['name']) && !empty($data['name'])) {
                $query->where('name', 'like',  '%' . $data['name'] . '%');
            }
            if (isset($data['code']) && !empty($data['code'])) {
                $query->where('code', 'like',  '%' . $data['code'] . '%');
            }
        };
    }
}
